package com.shbaiche.bifenba.adapter.home;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.shbaiche.bifenba.R;
import com.shbaiche.bifenba.model.video.ArticleVo;
import com.shbaiche.bifenba.ui.base.WebActivity;
import com.shbaiche.bifenba.ui.home.sub.SubActivity;
import com.shbaiche.bifenba.ui.video.VideoDetailActivity;

import java.util.List;

/**
 * FileName: HomeInfoAdapter
 * Author: 刘
 * Date: 2018/7/23 08:35
 * Description: ${DESCRIPTION}
 */
public class HomeInfoAdapter extends RecyclerView.Adapter<HomeInfoAdapter.ViewHolder> {

    private Activity mContext;

    private List<ArticleVo> list;

    public HomeInfoAdapter(Activity mContext, List<ArticleVo> list) {
        this.mContext = mContext;
        this.list = list;
    }

    public void clear() {
        if (list != null) {
            list.clear();
            notifyDataSetChanged();
        }
    }

    public void setData(List<ArticleVo> list){
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_info_item, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;

    }

    //对RecyclerView子项数据进行赋值
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (list == null || list.size() == 0) {
            return;
        }
        final ArticleVo item = list.get(position);
        if (item == null)
            return;
        boolean isAdv = item.getIsAdvert() != null ? item.getIsAdvert().getValue() : false;
        boolean isSpecial = item.getIsSpecial() != null ? item.getIsSpecial().getValue() : false;
        List<String> listImg = item.getImgUrl();
        int imgSize = listImg != null ? listImg.size() : 0;

        if (isAdv) {
            holder.info.setVisibility(View.GONE);
            holder.adv.setVisibility(View.VISIBLE);
            holder.sub.setVisibility(View.GONE);

            holder.titleAdv.setText(item.getTitle() + "");
            long ping = item.getBrowseNum() == null ? 0 : item.getBrowseNum();
            holder.descAdv.setText(ping + "球迷关注过");
            if (imgSize > 0) {
                Glide.with(mContext).load(listImg.get(0)).placeholder(R.drawable.defult).error(R.drawable.defult).into(holder.AdvImg);
            }

        } else if (imgSize > 1 || isSpecial) {
            holder.info.setVisibility(View.GONE);
            holder.adv.setVisibility(View.GONE);
            holder.sub.setVisibility(View.VISIBLE);

            holder.SubImg3.setVisibility(View.GONE);
            holder.SubImg2.setVisibility(View.GONE);

            holder.titleSub.setText(item.getTitle() + "");

            if (isSpecial) {
                holder.descSub.setText(item.getPublishTime() + "");
            } else {
                holder.descSub.setText(item.getSource() + "");
            }

            if (item.getIsTop() != null && item.getIsTop().getValue()) {
                holder.topSub.setVisibility(View.VISIBLE);
            } else {
                holder.topSub.setVisibility(View.GONE);
            }

            long ping = item.getDiscussNum() == null ? 0 : item.getDiscussNum();
            long lan = item.getBrowseNum() == null ? 0 : item.getBrowseNum();
            holder.pingSub.setText(ping + "评" + " " + lan + "浏览");


            for (int i = 0; i < listImg.size(); i++) {
                switch (i) {
                    case 0:
                        Glide.with(mContext).load(listImg.get(i)).placeholder(R.drawable.defult).error(R.drawable.defult).into(holder.SubImg1);

                        break;
                    case 1:
                        if (!isSpecial) {
                            holder.SubImg2.setVisibility(View.VISIBLE);
                            Glide.with(mContext).load(listImg.get(i)).placeholder(R.drawable.defult).error(R.drawable.defult).into(holder.SubImg2);
                        }
                        break;
                    case 2:
                        if (!isSpecial) {
                            holder.SubImg3.setVisibility(View.VISIBLE);
                            Glide.with(mContext).load(listImg.get(i)).placeholder(R.drawable.defult).error(R.drawable.defult).into(holder.SubImg3);
                        }
                        break;
                }
            }
        } else {
            holder.info.setVisibility(View.VISIBLE);
            holder.adv.setVisibility(View.GONE);
            holder.sub.setVisibility(View.GONE);
            holder.Infoimg.setVisibility(View.GONE);

            holder.titleInfo.setText(item.getTitle() + "");
            long ping = item.getDiscussNum() == null ? 0 : item.getDiscussNum();
            holder.timeInfo.setText(item.getSource() != null ? item.getSource() + " " + ping + "评":"");
            if (item.getIsTop() != null && item.getIsTop().getValue()) {
                holder.topInfo.setVisibility(View.VISIBLE);
            } else {
                holder.topInfo.setVisibility(View.GONE);
            }
            if (imgSize > 0) {
                holder.Infoimg.setVisibility(View.VISIBLE);
                Glide.with(mContext).load(listImg.get(0)).placeholder(R.drawable.defult).error(R.drawable.defult).into(holder.Infoimg);
            }else {
                holder.Infoimg.setVisibility(View.GONE);
            }
        }


        holder.adv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                WebActivity.launch(mContext, item.getContent(), "广告",item.getId().intValue());
            }
        });

        holder.info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Bundle b = new Bundle();
//                b.putLong("id", item.getId());
//                b.putString("type", "资讯");
//                VideoDetailActivity.launch((Activity) mContext, b);
                WebActivity.launch(mContext, item.getDetailUrl(), "资讯详情",item.getId().intValue());
            }
        });
        if (isSpecial) {
            holder.sub.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Bundle b = new Bundle();
                    b.putLong("id", item.getAuthorId());
                    SubActivity.launch(mContext, b);
                }
            });
        } else {
            holder.sub.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                    Bundle b = new Bundle();
//                    b.putLong("id", item.getId());
//                    b.putString("type", "资讯");
//                    VideoDetailActivity.launch((Activity) mContext, b);
                    WebActivity.launch(mContext, item.getDetailUrl(), "资讯详情",item.getId().intValue());
//                WebActivity.launch(mContext, "https://www.2cto.com/", "资讯详情");
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        if (list != null) {
            return list.size();
        }
        return 5;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout info, adv, sub;
        TextView titleInfo, topInfo, timeInfo, titleAdv, descAdv, titleSub, topSub, descSub, pingSub;
        ImageView Infoimg, AdvImg, SubImg1, SubImg2, SubImg3;

        public ViewHolder(View view) {
            super(view);
            info = (LinearLayout) view.findViewById(R.id.info_layout);
            adv = (LinearLayout) view.findViewById(R.id.adv_layout);
            sub = (LinearLayout) view.findViewById(R.id.sub_layout);
            titleInfo = (TextView) view.findViewById(R.id.info_title);
            topInfo = (TextView) view.findViewById(R.id.info_top);
            timeInfo = (TextView) view.findViewById(R.id.info_time_ping);
            Infoimg = (ImageView) view.findViewById(R.id.info_img);
            AdvImg = (ImageView) view.findViewById(R.id.adv_img);

            titleAdv = (TextView) view.findViewById(R.id.adv_title);
            descAdv = (TextView) view.findViewById(R.id.adv_desc);

            titleSub = (TextView) view.findViewById(R.id.sub_title);
            topSub = (TextView) view.findViewById(R.id.sub_top);
            descSub = (TextView) view.findViewById(R.id.sub_desc);
            pingSub = (TextView) view.findViewById(R.id.sub_ping);

            SubImg1 = (ImageView) view.findViewById(R.id.info_sub_img1);
            SubImg2 = (ImageView) view.findViewById(R.id.info_sub_img2);
            SubImg3 = (ImageView) view.findViewById(R.id.info_sub_img3);

        }
    }

}

